package main

import "fmt"

type Boy struct {
	no   int
	next *Boy
}

func addBoy(num int) *Boy {
	first := &Boy{}
	curr := &Boy{}
	if num < 1 {
		return first
	}
	for i := 1; i <= num; i++ {
		boy := &Boy{
			no: i,
		}

		if i == 1 {
			first = boy
			curr = boy
			curr.next = boy
		} else {
			curr.next = boy
			curr = boy
			curr.next = first // 构成环形链表
		}
	}
	return first
}

func list(first *Boy) {
	if first.next == nil {
		fmt.Println("链表为空")
		return
	}

	curr := first
	for {
		fmt.Println("no = ", curr.no)
		if curr.next == first {
			break
		}
		curr = curr.next
	}
}

func main() {
	first := addBoy(5)
	list(first)
	// 从第2个开始，数3下出列
	playGame(first, 2, 3)
}

func playGame(first *Boy, startNo int, countNum int) {
	if first.next == nil {
		fmt.Println("链表为空")
		return
	}
	// startNo  不可以大于总数

	tail := first
	// tail 需要指向尾部，方便删除
	for {
		if tail.next == first {
			break
		}
		tail = tail.next
	}

	// 移动到startNum
	for i := 1; i <= startNo-1; i++ {
		first = first.next
		tail = tail.next
	}
	// 开始数 countNum 下
	for true {
		for i := 1; i <= countNum-1; i++ {
			first = first.next
			tail = tail.next
		}
		// 开始删除
		fmt.Printf("编号%d 出圈 \n", first.no)
		first = first.next
		tail.next = first

		if tail == first {
			break
		}
	}
	fmt.Printf("最后出圈的是编号%d \n", first.no)

}
